package com.itheima.ydd.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvimgMapper;
import com.itheima.ydd.service.IYddAdvimgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.service.IYddAdvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements IYddAdvimgService {
@Autowired
private IYddAdvposService yddAdvposService;

    @Override
    public ResultData selectByPage(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        Page<YddAdvimg> page1 = new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
//        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc);
        wrapper.like(!StringUtils.isEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);

        Page<YddAdvimg> page2 = page(page1, wrapper);

//        YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
//        List<YddAdvimgDto> objects = new ArrayList<>();
        page2.getRecords().stream().map((item)->{
            YddAdvpos byId = yddAdvposService.getById(item.getAdvimgpos());
            item.setAdvposname(byId.getAdvposname());
            return item;
        }).collect(Collectors.toList());

        HashMap<Object, Object> map = new HashMap<>();
        map.put("count",page2.getCountId());
        map.put("totalPages",page2.getTotal());
        map.put("pageSize",page2.getSize());
        map.put("currentPage",page2.getCurrent());
        map.put("data",page2.getRecords());
        return ResultData.ok(null,map);
    }

    @Override
    public ResultData selectByselectByall() {
        List<YddAdvimg> list = list();
        list.stream().map((item)->{
            YddAdvpos byId = yddAdvposService.getById(item.getAdvimgpos());
            item.setAdvposname(byId.getAdvposname());
            return item;
        }).collect(Collectors.toList());
        return ResultData.ok(null,list);
    }

    @Override
    public ResultData rowInfo(Integer id) {
        YddAdvimg byId = getById(id);
        return ResultData.ok(null,byId);
    }

    //修改
    @Override
    public void updateyddArt(YddAdvimg yddAdvimg) {
        try {
            updateById(yddAdvimg);
        } catch (Exception e) {
            throw new BusinessException("修改失败");
        }
    }

    @Override
    public void add(YddAdvimg yddAdvimg) {
        try {
            save(yddAdvimg);
            LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YddAdvimg::getAdvimgpos,yddAdvimg.getAdvimgpos());
            int num = list(wrapper).size();
            String id = yddAdvimg.getAdvimgpos();

            YddAdvpos yddAdvpos = new YddAdvpos();
            yddAdvpos.setId(Integer.valueOf(id));
            yddAdvpos.setAdvposnum(num+"");
            yddAdvposService.updateById(yddAdvpos);
        } catch (Exception e) {
            throw new BusinessException("新增失败");
        }
    }

    @Override
    public List<YddAdvimg> getTu(Integer id) {
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvimg::getAdvimgpos,id);
        List<YddAdvimg> list = list(wrapper);
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        YddAdvimg yddAdvimg = getById(id);
        removeById(id);

        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvimg::getAdvimgpos,yddAdvimg.getAdvimgpos());
        int num = list(wrapper).size();
        String idd = yddAdvimg.getAdvimgpos();
        YddAdvpos yddAdvpos = new YddAdvpos();
        yddAdvpos.setId(Integer.valueOf(idd));
        yddAdvpos.setAdvposnum(num+"");
        yddAdvposService.updateById(yddAdvpos);
    }
}
